package com.vista.questionnaire.domain;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

import javax.persistence.*;
import javax.validation.constraints.*;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.*;

/**
 * A Examination.
 */
@Entity
@Table(name = "examination")
//@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Examination implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull
    @Column(name = "title", nullable = false)
    private String title;

    @Column(name = "url")
    private String url;

    @Lob
    private String content;//html

    @LazyCollection(LazyCollectionOption.FALSE)//這裏不需要嬾加載
    @OneToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST})
    @JoinTable(name = "REF_EXAMINATION_QUESTION",
        joinColumns = @JoinColumn(name = "EXAMINATION_ID",
            referencedColumnName = "ID"),
        inverseJoinColumns = @JoinColumn(name = "QUESTION_ID",
            referencedColumnName = "ID"))
//    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private List<Question> questions = new ArrayList<>();

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime startTime;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime stopTime;


    public LocalDateTime getStartTime() {
        return startTime;
    }

    public void setStartTime(LocalDateTime startTime) {
        this.startTime = startTime;
    }

    public LocalDateTime getStopTime() {
        return stopTime;
    }

    public void setStopTime(LocalDateTime stopTime) {
        this.stopTime = stopTime;
    }

    // jhipster-needle-entity-add-field - JHipster will add fields here, do not remove
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public Examination title(String title) {
        this.title = title;
        return this;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getUrl() {
        return url;
    }

    public Examination url(String url) {
        this.url = url;
        return this;
    }

    public void seUrl(String url) {
        this.url = url;
    }

    @JsonProperty
    public List<Question> getQuestions() {
        return questions;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Examination questions(List<Question> questions) {
        this.questions = questions;
        return this;

    }

//    public Examination addQuestions(Question question) {
//        this.questions.add(question);
//        question.setExamination(this);
//        return this;
//    }
//
//    public Examination removeQuestions(Question question) {
//        this.questions.remove(question);
//        question.setExamination(null);
//        return this;
//    }

    @JsonIgnore
    public void setQuestions(List<Question> questions) {
        this.questions = questions;
    }
    // jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here, do not remove


    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Examination examination = (Examination) o;
        if (examination.getId() == null || getId() == null) {
            return false;
        }
        return Objects.equals(getId(), examination.getId());
    }

}
